package com.model.controller;

import com.entity.LogEvent;
import com.mapper.LogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Aop {
    @Autowired
    LogMapper logMapper;

    @Pointcut(value = "execution(* com.model.service.*Service.*(..))")
    public void pointcut(){};

    @Around(value = "pointcut()")
    public Object notice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long pre = System.currentTimeMillis();
        LogEvent logEvent = new LogEvent();
        //logEvent.setMethod(proceedingJoinPoint.getSignature());
        Object obj = proceedingJoinPoint.proceed();
        long last = System.currentTimeMillis();
        logEvent.setMethod(proceedingJoinPoint.getSignature().getName());
        logEvent.setLastTime(last-pre);
        logMapper.insertLog(logEvent);
        return obj;
    }
}
